<template>
  <div>
    <div class="search">
      <el-input placeholder="请输入人名查询" style="width: 200px" v-model="name"></el-input>


<!--      <el-select  placeholder="请输入家谱名查询" style="width: 30%" v-model="familyName">-->
<!--        <el-option v-for="item in family" :key="item.id" :value="item.id" :label="item.name"></el-option>-->
<!--      </el-select>-->

<!--      <el-input placeholder="请输入家谱名查询" style="width: 200px; margin-left: 20px" v-model="familyName"></el-input>-->


      <el-button type="info" plain style="margin-left: 10px" @click="load(1)">查询</el-button>
      <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
    </div>

    <div class="operation">
      <el-button type="primary" plain @click="handleAdd">新增</el-button>
      <el-button type="danger" plain @click="delBatch">批量删除</el-button>
    </div>

    <div class="table">
      <el-table :data="tableData" strip @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="序号" width="70" align="center" sortable></el-table-column>
        <el-table-column prop="familyName" label="所属家谱"></el-table-column>
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column label="头像">
          <template v-slot="scope">
            <div style="display: flex; align-items: center">
              <el-image style="width: 40px; height: 40px; border-radius: 50%" v-if="scope.row.avatar"
                        :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]"></el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="sex" label="性别"></el-table-column>
        <el-table-column prop="birth" label="出生时间"></el-table-column>
        <el-table-column prop="death" label="死亡时间"></el-table-column>
        <el-table-column prop="des" label="备注"></el-table-column>
        <el-table-column prop="time" label="修改时间"></el-table-column>
        <el-table-column label="生平" align="center" width="90" >
          <template v-slot="scope">
            <el-button size="mini" type="primary" plain @click="loadDetail(scope.row.id)">查看详情</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template v-slot="scope">
            <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" plain @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[5, 10, 20]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total">
        </el-pagination>
      </div>
    </div>


    <el-dialog title="编辑信息" :visible.sync="fromVisible" width="60%" :close-on-click-modal="false" destroy-on-close>
      <el-form :model="form" label-width="100px" style="padding-right: 50px" :rules="rules" ref="formRef">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="所属家谱" prop="familyId">
          <el-select style="width: 100%" v-model="form.familyId">
            <el-option v-for="item in family" :key="item.id" :value="item.id" :label="item.name"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="父亲" prop="parentId">
          <el-select style="width: 100%" v-model="form.parentId">
            <el-option v-for="item in member" :key="item.id" :value="item.id" :label="item.name"></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
            <el-radio label="不详"></el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="出生时间" prop="birth">
          <el-radio-group v-model="form.birth">
            <el-radio label="不详"  style=" margin-right: 158px"></el-radio>
            <el-radio>
            <el-date-picker format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                            v-model="form.birth" style="width: 100%"></el-date-picker>
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="死亡时间" prop="death">
          <el-radio-group v-model="form.death">
            <el-radio label="不详" style=" margin-right: 60px"></el-radio>
            <el-radio label="无" style=" margin-right: 60px" ></el-radio>
            <el-radio>
              <el-date-picker format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                              v-model="form.death" style="width: 100%"></el-date-picker>
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="des">
          <el-input v-model="form.des" placeholder="备注"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-upload
              class="avatar-uploader"
              :action="$baseUrl + '/files/upload'"
              :headers="{ token: user.token }"
              list-type="picture"
              :on-success="handleAvatarSuccess"
          >
            <el-button type="primary">上传头像</el-button>
          </el-upload>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="fromVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>



    <el-dialog title="生平信息" :visible.sync="fromVisible1" width="60%" :close-on-click-modal="false" destroy-on-close>
      <el-table :data="event" strip @selection-change="handleSelectionChange">
<!--        <el-table-column prop="memberId" label="ID" align="center"></el-table-column>-->
        <el-table-column prop="eventTime" label="事件时间" ></el-table-column>
        <el-table-column prop="eventName" label="事件名称"></el-table-column>
        <el-table-column prop="eventAvatar" label="事件图片">
          <template v-slot="scope">
            <div style="display: flex; align-items: center">
              <el-image style="width: 40px; height: 40px;" v-if="scope.row.eventAvatar"
                        :src="scope.row.eventAvatar" :preview-src-list="[scope.row.eventAvatar]"></el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="eventDes" label="事件描述" align="center"></el-table-column>

      </el-table>
      <div slot="footer" class="dialog-footer">
<!--        <el-button size="mini" type="primary" plain @click="handleEdit1(scope.row)">编辑</el-button>-->
        <el-button type="primary" @click="fromVisible1 = false">确 定</el-button>
      </div>
    </el-dialog>




  </div>
</template>

<script>
export default {
  name: "Member",
  data() {
    return {
      tableData: [],  // 所有的数据
      family:[],
      member:[],
      pageNum: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      name: null,
      familyName:null,
      eventName:null,

      fromVisible: false,
      fromVisible1: false,

      form: {},
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      rules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
        ],
        familyId:[
          {required: true, message: '请选择所属家谱', trigger: 'blur'},
        ],

        sex: [
          {required: true, message: '请输入性别', trigger: 'blur'},
        ]



      },
      ids: [],
      event:[],
    }
  },
  created() {
    this.load(1)

  },
  methods: {
    loadDetail(id) {
      this.$request.get('/event/selectAll', {
        params: {
          memberId: id
        }
      }).then(res => {
        this.event = res.data
        this.fromVisible1 = true
      })
    },
    handleAdd() {   // 新增数据
      this.form = {}  // 新增数据的时候清空数据
      this.fromVisible = true   // 打开弹窗

    },
    handleEdit(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.fromVisible = true   // 打开弹窗
    },




    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request({
            url: this.form.id ? '/member/update' : '/member/add',
            method: this.form.id ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {  // 表示成功保存
              this.$message.success('保存成功')
              this.load(1)
              this.fromVisible = false
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    del(id) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/member/delete/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    handleSelectionChange(rows) {   // 当前选中的所有的行数据
      this.ids = rows.map(v => v.id)
    },
    delBatch() {   // 批量删除
      if (!this.ids.length) {
        this.$message.warning('请选择数据')
        return
      }
      this.$confirm('您确定批量删除这些数据吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/member/delete/batch', {data: this.ids}).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/member/selectPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
          familyName: this.familyName,
          familyId : this.familyId,
          eventName: this.eventName,
        }
      }).then(res => {
        this.tableData = res.data?.list
        this.total = res.data?.total
      })

      this.$request.get('/family/selectAll').then(res => {
        this.family = res.data || []
      })
      this.$request.get('/member/selectAll').then(res => {
        this.family = res.data || []
      })
      this.$request.get('/event/selectAll').then(res => {
        this.event = res.data || []
      })

    },

      reset() {
      this.name = null
      this.familyName = null
      this.eventName = null

      this.load(1)
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
    handleAvatarSuccess(response, file, fileList) {
      // 把头像属性换成上传的图片的链接
      this.form.avatar = response.data
    },
  }
}
</script>

<style scoped>

</style>